#include "config.h"
#include "feature_analyzer.h"

#include <cstdio>
#include <cstring>

#include <string>

#include <errno.h>

#include <time.h>

#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>


using namespace std;

int main(int argc, char **argv){

    if (argc != 2){
        printf("Usage: dataGen <working directory>\n");
        exit(0);
    }

    string directory = string(argv[1]);
    if (*(directory.rbegin()) != '/') {
        directory +=  "/";
    }


    Config config;
    if (config.ReadConfigFile(directory) < 0) {
        return 0;
    }
   
    FeatureAnalyzer fa(&config);   
    char filename[1024];
    char fullpath[1024];
    while (fscanf(config.binaryFile, "%s", filename) != EOF){
	strcpy(fullpath, (directory + string("/binary/") + string(filename)).c_str());		
//	printf("The number of opened files %d\n", OpenedFiles());

        clock_t start = clock();
	clock_t diff;
	printf("Analyzing %s\n", fullpath);
	fa.Analyze(fullpath);
	diff = clock()- start;
	int mesc = diff * 1000 / CLOCKS_PER_SEC;
	printf("Time taken %d s %d ms\n", mesc / 1000, mesc % 1000);
//	break;
    }

//    for (auto iter = fa.funcCount.begin(); iter != fa.funcCount.end(); ++iter)
//        printf("%s %d\n", iter->first.c_str(), iter->second);


    fa.totalStat.Print(config.statisticFile, "total info");
    fa.PrintFeatureList();
    config.Close();

}
